package org.argus.job.server.system.management.service;

import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.argus.common.core.utils.JsonUtils;
import org.argus.job.common.SJ;
import org.argus.job.common.model.AlarmConfig;
import org.argus.job.common.model.JobAdvancedRuntimeConfig;
import org.argus.job.common.model.LifeCycle;
import org.argus.job.common.model.LogConfig;
import org.argus.job.common.request.http.SaveJobInfoRequest;
import org.argus.job.server.system.domain.JobInfoEntity;
import org.springframework.beans.BeanUtils;

import com.google.common.collect.Lists;

/**
 * JobConverter
 *
 * @author tjq
 * @since 2023/3/4
 */
public class JobConverter {

    public static SaveJobInfoRequest convertJobInfoEntity2SaveJobInfoRequest(JobInfoEntity jobInfo) {
        SaveJobInfoRequest saveJobInfoRequest = new SaveJobInfoRequest();
        BeanUtils.copyProperties(jobInfo, saveJobInfoRequest);
        saveJobInfoRequest.setTimeExpressionType(jobInfo.getTimeExpressionType());
        saveJobInfoRequest.setExecuteType(jobInfo.getExecuteType());
        saveJobInfoRequest.setProcessorType(jobInfo.getProcessorType());
        if (StringUtils.isNotEmpty(jobInfo.getNotifyUserIds())) {
            saveJobInfoRequest.setNotifyUserIds(Lists.newArrayList(SJ.COMMA_SPLITTER.split(jobInfo.getNotifyUserIds())).stream().map(Long::valueOf).collect(Collectors.toList()));
        }
        saveJobInfoRequest.setDispatchStrategy(jobInfo.getDispatchStrategy());
        saveJobInfoRequest.setLifeCycle(LifeCycle.parse(jobInfo.getLifecycle()));
        saveJobInfoRequest.setAlarmConfig(JsonUtils.parseObjectIgnoreException(jobInfo.getAlarmConfig(), AlarmConfig.class));
        saveJobInfoRequest.setLogConfig(JsonUtils.parseObjectIgnoreException(jobInfo.getLogConfig(), LogConfig.class));
        saveJobInfoRequest.setAdvancedRuntimeConfig(JsonUtils.parseObjectIgnoreException(jobInfo.getAdvancedRuntimeConfig(), JobAdvancedRuntimeConfig.class));
        return saveJobInfoRequest;
    }

}
